package water.com.Log;

import water.com.Observer.Observer;
import water.com.Observer.Subject;

import java.util.ArrayList;
import java.util.List;

public class LogSubject implements Subject {

    //存储订阅者
    private final List<Observer> observerList;
    private Log log;

    public void setLog(Log log){
        this.log = log;
    }
    public LogSubject(){
        observerList = new ArrayList<>();
    }
    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int i = observerList.indexOf(observer);
        if(i>=0) observerList.remove(i);

    }

    @Override
    public void notifyObservers() {
        for(Observer observer:observerList){
            observer.update(log);
        }
    }
}
